# In sampler_worker_script.py

import pandas as pd
from analyze_mutations import analyze_mutations_dataframe 
from contig_data import ContigData




def sampler_function(df, sample_size):
    sampled_df = df.sample(n=sample_size, replace=True)
    return sampled_df



def worker_function(task_id, contig_ranges, consecutive_positions, df, return_dict):
    try:
        #Initialize container
        contig_data = ContigData()
        contig_data.initialize_contigs(contig_ranges, consecutive_positions)

        # Analyze the subsample
        analyzed_df = analyze_mutations_dataframe(df)

        # Iterate through each row of the DataFrame
        for index, row in analyzed_df.iterrows():
            # Extract the necessary information from each row
            mutation_details = {
                'perfect_match': row['perfect_match'],
                'ambiguous_deletions': row['ambiguous_deletions'],
                'insertions': row['insertions'],
                'deletions': row['deletions'],
                'substitutions': row['substitutions']
            }

            contig = row['contig']  # Extract the contig information

            if mutation_details['perfect_match']:
                contig_data.update_simple_count(contig, 'WT')  # Make sure to pass 'contig' instead of 'contig_data'
                continue

            if mutation_details['ambiguous_deletions']:
                contig_data.update_simple_count(contig, 'ambiguous_deletions')
                continue

            if mutation_details['insertions']:
                contig_data.update_simple_count(contig, 'insertions')
                continue

            for position in mutation_details['deletions']:
                contig_data.update_deletion(contig, position)

            for (position, base) in mutation_details['substitutions']:
                contig_data.update_substitution(contig, position, base)

        # Save the result in the shared dictionary using task_id as the key
        return_dict[task_id] = contig_data

    except Exception as e:
        print(f"An error occurred in worker_function: {e}")
        return_dict[task_id] = None  # Or some error indicator
